<?php

/*
*author:段帅
*time:2018/7/5 上午11:29
*description:
*/

/**
 * Created by PhpStorm.
 * User: duanshuai
 * Date: 2018/7/5
 * Time: 上午11:29
 */

namespace app\index\controller;
use think\Db;
use think\request;
use app\index\model;




class Base
{


    /**
     * @Notes:
     * @User: 段帅
     * @Time: 2018/7/5  下午2:13
     * @param $alert  提示信息
     * @param array $data  返回data
     * @return array
     */
    public function dataFormate_0($alert,$data=[]){
        return ['status'=>0,'statusCode'=>$alert,'data'=>$data];
     }

    public function dataFormateList_0($alert,$data=[],$total){
        return ['status'=>0,'statusCode'=>$alert,'data'=>$data,'total'=>$total];
    }

    public function dataFormate_1($alert,$data=[]){
        return ['status'=>1,'statusCode'=>$alert,'data'=>$data];

    }


    /**
     * @Notes: 校验参数和token
     * @User: 段帅
     * @Time: 2018/7/5  下午1:46
     * @param $para  需要校验的参数
     * @param $mod  字段
     */
    public function checkParamsWithToken($para,$mod=[]){
        array_push($mod,'token','token_secret');

        $para = array_filter($para);//去除空
        foreach ($mod as $val) {
            if (empty($para[$val])){
                return ['result'=>false,'mes'=>"缺少 $val 参数"];
            }

        }
        //试试联合查询
        $result = Db::name('login') -> where(['token'=>$para['token'],'token_secret'=>$para['token_secret']])->find();
        if ($result){
            $id = Db::name('user') -> where('count',$result['count'])->value('token_id');
            if ($id == $result['id']&&time()<$result['out_time']){
                 $result = array_merge($result,$para);
                unset($result['id'],$result['token'],$result['token_secret'],$result['create_time'],$result['out_time']);
                return array_merge(['result'=>true],$result);
            }else{
                return ['result'=>false,'mes'=>"token_s过期"];
            }
        }else{
            return ['result'=>false,'mes'=>"认证失败"];
        }




        //校验token




    }


    /**
     * @Notes:校验参数
     * @User: 段帅
     * @Time: 2018/7/5  下午1:47
     * @param $para
     * @param $mod
     */
    public function checkParamsNoToken($para,$mod){

        $para = array_filter($para);//去除空
        foreach ($mod as $val) {
            if (empty($para[$val])){
                return false;
            }

        }
        return true;


    }

    /**
     * @Notes:生成token和token_secret
     * @User: 段帅
     * @Time: date_ds
     * @param $count  账号
     * @param $password 密码
     * @return array
     */
    public function crateTokenAndSctoken($count,$password){
        $data = [];
        $data['token'] = md5($count+$password);
        $data['token_secret'] = md5($count+$password+time());
        return $data;
    }







}